﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasRecordStatusDAL : IMasRecordStatusDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasRecordStatusDAL()
        {
        }
        public IList<MasRecordStatusInfo> ListRecordStatus(MasRecordStatusInfo rsInfo)
        {
            string strSQL = "";

            List<MasRecordStatusInfo> infoList = new List<MasRecordStatusInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@Status", rsInfo.Status);

                if (rsInfo.Type == SystemInfo._StatusCommand)
                {
                    strSQL = "SELECT RECORD_SEQ, RECORD_STATUS, CMD_STATUS_NAME"
                        + " FROM MAS_RECORD_STATUSES"
                        + " WHERE RECORD_STATUS IN ('A', 'W')"
                        + (rsInfo.Status.Length > 0 ? " AND RECORD_STATUS = @Status" : "")
                            + " ORDER BY RECORD_SEQ";
                }
                else 
                {
                    strSQL = "SELECT RECORD_SEQ, RECORD_STATUS, RECORD_STATUS_NAME"
                       + " FROM MAS_RECORD_STATUSES"
                       + " WHERE RECORD_STATUS IN ('U', 'C')"
                       + (rsInfo.Status.Length > 0 ? " AND RECORD_STATUS = @Status" : "")
                           + " ORDER BY RECORD_SEQ";
                }

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasRecordStatusInfo Info = new MasRecordStatusInfo(rdr.GetInt32(0)
                        , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                        , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                        , ""
                        );

                    infoList.Add(Info);
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        
    }
}
